package playing.action;

import playing.*;

import java.util.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class friends {
    private int userId;
    private JFrame frame;

    public friends(int id) {
        this.userId = id;
        setupGUI();
    }

    private void setupGUI() {
        frame = new JFrame("好友互动界面");
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        frame.add(panel);

        JLabel label = new JLabel("选择操作：");
        panel.add(label);

        JButton allFriendsButton = new JButton("显示全部好友");
        panel.add(allFriendsButton);

        JButton interactButton = new JButton("好友互动");
        panel.add(interactButton);

        JButton addOrDeleteButton = new JButton("添加/删除好友");
        panel.add(addOrDeleteButton);

        JButton returnButton = new JButton("返回主页面");
        panel.add(returnButton);

        allFriendsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showAllFriends();
            }
        });

        interactButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                operate();
            }
        });

        addOrDeleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new friends_add_or_delete(userId);
                frame.dispose();
            }
        });

        returnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //new menu(userId);
                frame.dispose();
            }
        });

        frame.setVisible(true);
    }

    private void showAllFriends() {
        List<Integer> friendsList = new PlayerFriendships().getAllFriendIds(userId);
        if (!friendsList.isEmpty()) {
            StringBuilder info = new StringBuilder("以下是你的好友信息:\n");
//            for (int userId : friendsList) {
//                System.out.println(userId);
//            }
            new showfrends().printAllUsersByIdList(friendsList);

            JOptionPane.showMessageDialog(frame, info.toString());
        } else {
            JOptionPane.showMessageDialog(frame, "你还没有一个好友，快添加吧");
        }
    }

    private void operate() {
        String input = JOptionPane.showInputDialog(frame, "请输入你想要互动的朋友的ID:");
        if (input != null && !input.isEmpty()) {
            try {
                int friendId = Integer.parseInt(input);
                if (friendId == userId) {
                    JOptionPane.showMessageDialog(frame, "不能和自己互动");
                } else if (new PlantLogin().userExistsById(friendId)) {
                    if (new PlayerFriendships().isFriend(userId, friendId)) {
                        new friends_menu(friendId, userId);
                        frame.dispose();
                    } else {
                        JOptionPane.showMessageDialog(frame, "你和他还不是好友");
                    }
                } else {
                    JOptionPane.showMessageDialog(frame, "查无此人");
                }
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(frame, "请输入有效的ID");
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new friends(1); // 假设用户ID为1
            }
        });
    }
}
